function load_termsOfServices() {
    $.get("notes/terms.txt", {}, function(doc) {
        var procDoc = "<p>" + doc.replace(/^\s+/, "").replace(/\s+$/, "").replace(/\s\s+/g, "</p><p>") + "</p>";
        $("#terms div").html(procDoc);
    });
}

function installEventListeners() {
    // Disabled functions
    //$("#a-password-retrieval").remove();
    
    $(window).resize(dialog.autoPosition);
    
    var noDraggingArea = "body";
    var exceptionalArea = "input, textarea";
    //var noDraggingArea = "a, h1, h2, h3, p, label, #controller, #directories, #files";
    if (!$.support.opacity) {
        $(noDraggingArea).not(exceptionalArea).attr("unselectable", "on");
    } else {
        $(noDraggingArea).not(exceptionalArea).live("mousedown", actions.noOperation);
        $(noDraggingArea).not(exceptionalArea).live("mouseup", actions.noOperation);
    }
    
    // General triggers
    $("#controller a").click(actions.controller.selectCommand);
    
    $("#dialog-close").click(dialog.hide);
    
    // General UI restriction (UI)
    $("a, h1, h2, h3, p, label, #controller").live("mousedown", actions.noOperation);
    $("a, h1, h2, h3, p, label, #controller").live("mouseup", actions.noOperation);
    
    // Triggers: Sign-in, Password Retrieval and Registration
    $("#a-signin, #a-password-retrieval, #a-signup").click(actions.controller.switchToNAUTHMode);
    $("#signin form").submit(user.authorize);
    
    // Uploader: Submit files
    $("#uploader form").submit(actions.filesystem.upload);
    
    // Registration: Terms
    $("#terms").submit(actions.user.signAgreement);
    
    // Registration: Form
    $("#registrationForm").submit(actions.user.register);
    
    // Trigger: Sign-out
    $("#a-signout").click(user.removeAuthorization);
    
    // Trigger: Browse
    $("#a-browse").click(actions.filesystem.browse);
    
    // Trigger: Create new folder
    $("#a-create-folder").click(ui.dialog.createFolder);
    $("#create-folder form").submit(actions.filesystem.makeFolder);
    
    // Trigger: Select all items
    $("#a-select-all").click(ui.items.selectAll);
    // Trigger: Select none
    $("#a-select-none").click(ui.items.selectNone);
    
    // Trigger: Reset stock
    $("#a-reset-stock").click(ui.items.clearStock);
    
    // Trigger: Add files
    $("#a-add-files").click(actions.controller.promptToUpload);
    
    // Trigger: Site information
    $("#a-site-info").click(actions.controller.showSiteInfo);
    
    // Trigger: About the system
    $("#a-sysabout").click(actions.controller.showAbout);
    
    // Trigger: Language Switch
    $("#a-language-switch").click(actions.controller.showLanguageSwitch);
    $("#language-switch button").click(actions.controller.doLanguageSwitch)
    
    // Trigger: Browser / Select items
    $("#browser").find("#files .file, #directories .directory").live("click", ui.items.clickItem);
    $("#browser-stock").find(".commands .c-a-remove-du-stock").live("click", ui.items.removeItemInStock);
    
    // Trigger: Browser / Access the directory
    $("#browser").find("#directories .directory").live("dblclick", actions.filesystem.browse);
    $("#browser a.location-path").live("click", actions.filesystem.browse);
    
    // Trigger: Browser / Display stock
    $("#browser #a-stock").bind("click", actions.stock.toggle);
    
    // Trigger: Browser / Move files or directories
    $("#browser #a-move").bind("click", actions.filesystem.move);
    
    // Trigger: Browser / Delete files or directories
    $("#browser #a-delete").bind("click", actions.filesystem.remove);
    
    // Widget Installation: File Filter
    $("#browser-filter").live('submit', actions.noOperation);
    $("#browser-filter").live("change", actions.noOperation);
    $("#browser-filter input").change(ui.items.filter);
    $("#browser-filter input").keyup(ui.items.filter);
    $("#browser-filter input").keydown(ui.items.filter);
    $("#browser-filter input").keypress(ui.items.filter);
    $("#browser-filter a").click(ui.items.filterCancel);
    
    // Widget: Renaming
    $("#browser .fn input, #browser .dn input").live("keypress", actions.filesystem.rename);
    $("#browser-stock .fn, #browser-stock .dn").live("click", actions.filesystem.renameEnabled);
    $("#browser .fn input, #browser .dn input").live("keyup", actions.filesystem.renameCancelWithEscKey);
    $("#browser .fn button, #browser .dn button").live("click", actions.filesystem.renameCancelWithButton);
    
    // Widget: Sneakpeak
    $("#browser").find(".file, .directory").live("mouseover", ui.items.showInfo);
    $("#browser").find(".file, .directory").live("mouseout", ui.items.hideInfo);
    
    // Widget: Mode switch
    $("#a-view-be").live("click", function (event) {
        $("#browser").removeClass("view-tn");
        $("#browser-mode a").removeClass("e-selected");
        $(this).addClass("e-selected");
    });
    
    $("#a-view-tn").live("click", function (event) {
        $("#browser").addClass("view-tn");
        $("#browser-mode a").removeClass("e-selected");
        $(this).addClass("e-selected");
    });
    
    $("body").bind("mousemove", ui.items.repositionInfoPane);
}

$(document).ready(function () {
    // Localization
    locale = $.cookie("locale");
    try {
        if (String(location.href).match(/\?.+$/)) {
            locale = String(location.href).substring(String(location.href).indexOf("?") + 1);
            $.cookie("locale", locale);
        }
    } catch (e) {
        locale = $.cookie("locale");
    }
    if (locale == null || knownLocales.indexOf(locale) == -1) {
        locale = defaultLocale;
    } else {
        localizeUI(locale);
    }
    dictionary.locale = dictionary[locale];
    
    // Setup UI for Language Switcher
    $.each(knownLocales, function(index) {
        var lcode = knownLocales[index];
        $("#language-switch ul").append('<li><button value="' + lcode + '"' + (lcode == locale?' disabled="disabled"':'') + '>' + dictionary[lcode].__label + '</button></li>');
    });
    
    // Normal Startup
    load_termsOfServices();
    installEventListeners();
    if (user.authorized()) {
        ui.state.userAuthorizationGranted();
    } else {
        ui.state.userAuthorizationDenied();
    }
    $.getScript('sessions?identifier', function() {});
    $("#controller-underlay").css("background-color", "#000").animate(
        {
            height: "33px",
            opacity: 0.2
        }, 500
    );
});